package com.aitesting.controller;


import com.aitesting.common.CommonPage;
import com.aitesting.common.CommonResult;
import com.aitesting.dto.ApiDto;
import com.aitesting.model.Api;
import com.aitesting.model.ApiGroup;
import com.aitesting.model.User;
import com.aitesting.service.ApiGroupService;
import com.aitesting.service.ApiService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 *接口管理
 * @author zz
 * @since 2022-02-20
 */
@RestController
@RequestMapping("/api")
@Slf4j
public class ApiController {

    @Autowired
    private ApiService apiService;

    @Autowired
    private ApiGroupService apiGroupService;

    /**
     * 根据id查询接口信息
     * @param id 接口id
     * @return CommonResult(code,message,data)
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public CommonResult<Api> getById(@PathVariable("id") Integer id) {
        return CommonResult.success(apiService.getById(id));
    }

    /**
     * 查询所有接口信息
     * @return CommonResult(code,message,data)
     */
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    public CommonResult<Api> getList() {
        return CommonResult.success(apiService.list());
    }

    /**
     * 新增接口
     * @param api 接口对象
     * @return CommonResult(code,message,data)
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult<Api> create(@RequestBody Api api){
        boolean isOk = apiService.save(api);
        if (isOk) {
            log.debug("创建接口成功 {}", api);
            return CommonResult.success(api);
        }else {
            log.error("新增接口失败,数据不合法 {}", api);
            return CommonResult.failed();
        }
    }

    /**
     * 根据id删除接口
     * @param id 接口id
     * @return CommonResult(code,message,data)
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public CommonResult<Api> delete(@PathVariable Integer id) {
        boolean isOk = apiService.removeById(id);
        if (isOk) {
            return CommonResult.success(null);
        }else {
            return CommonResult.failed();
        }
    }

    /**
     * 根据id更新接口信息
     * @param api 接口对象
     * @return CommonResult(code,message,data)
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public CommonResult<Api> update(@RequestBody Api api) {
        boolean isOk = apiService.updateById(api);
        if (isOk) {
            log.debug("更新接口成功 {}", api);
            return CommonResult.success(api);
        }else {
            log.error("更新接口失败,数据不合法 {}", api);
            return CommonResult.failed();
        }
    }

    /**
     * 分页查询分组信息
     * @param pageIndex 第几页
     * @param pageSize 每页大小
     * @return CommonResult(code,message,data)
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<Api> listPage(@RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        Page<Api> page = new Page<>(pageIndex, pageSize);
        Page<Api> apiPage = apiService.page(page);
        return CommonResult.success(CommonPage.restPage(apiPage));
    }
}

